import fetchJsonp from './fetchJsonp'
import { staticUrl, uleUrl } from '../common/env'

String.prototype.sliceAfter = function(str) {
  return (this.indexOf(str) >= 0) ? this.substring(0, this.indexOf(str)) : '';
};
String.prototype.sliceBefore = function (str) {
  return(this.indexOf(str) >= 0) ?this.substring(this.indexOf(str) + str.length, this.length) : '';
};

/**
 * 获取浏览器属性
 */
const browserFunc = () => {
  let ua = navigator.userAgent.toLowerCase(),
    os, version;
  if (ua.indexOf('uleapp/') > 0) {
    version = ua.sliceAfter('uleapp/').split('_')[3];
    os = ua.sliceAfter('uleapp/').sliceBefore('_');
    if (ua.sliceAfter('uleapp/').split('_')[1] == 'ule') {
      let uappType = {
        ule: true,
        ylxd: false
      };
    } else {
      let uappType = {
        ule: false,
        ylxd: true
      };
    }
    let appobj = Object.assign({
      ios: os == 'ios',
      android: os == 'android',
      version: version
    }, uappType);
    return appobj;
  } else if (ua.indexOf('ulxdapp/') > 0) {
    os = ua.sliceAfter('ulxdapp/').sliceBefore('_');
    return {
      ylxd: true,
      wx: false,
      ios: os == 'ios',
      android: os == 'android'
    };
  } else if (ua.indexOf('uzgapp/') > 0) {
    version = ua.sliceAfter('uzgapp/').split('_')[3];
    os = ua.sliceAfter('uzgapp/').sliceBefore('_');
    return {
      uzg: true,
      wx: false,
      ios: os == 'ios',
      android: os == 'android',
      version: version
    };
  } else {
    return {
      ule: false,
      uzg: false,
      ylxd: false,
      wx: ua.match(/micromessenger/i),
      ios: ua.match(/(iphone|ipod|ipad);?/i),
      android: ua.match(/android/i)
    };
  }
}
/**
 * getQueryJson
 */
const  getQueryJson = (hrefstr) => {
    if (hrefstr.indexOf('?') < 0)
      return {};
    var query = hrefstr.substr(hrefstr.indexOf('?') + 1), params = query.split('&'), len = params.length, result = {}, key, value, item, param;
    for (var i = 0; i < len; i++) {
      param = params[i].split('=');
      key = param[0];
      value = param[1];
      item = result[key];
      if ('undefined' == typeof item) {
        result[key] = value
      } else if (Object.prototype.toString.call(item) == '[object Array]') {
        item.push(value)
      } else {
        result[key] = [item, value]
      }
    }
    return result
  }
  /**
   * 获取cookie
   * @param name 
   */
const getCookie = (name) => {
    var cookies = document.cookie.split(";");
    for(var i = 0,len = cookies.length;i<len;i++){
        if(cookies[i].split('=')[0].trim() == name){
            return cookies[i].split('=')[1];
        }
    } 
}
const checkUserStatus = () => {
  if (getCookie('mall_cookie')) {
    return true;
  } else {
    return false;
  }
}
const isLogin = (callback) => {
  if (checkUserStatus()) {
    callback && callback.apply(this);
  } else {
    if (browser.ule) {
      //APP登录
      location.href = 'uleMobile://uleLogin_' + location.href;
    } else {
      //wap登录
      location.href = 'https://m.' + uleUrl + '/user/login?target=' + encodeURIComponent(location.href);
    }
  }
}
const browser = browserFunc();
console.log(browser)
const tipBox = (msg) => {
  // alert提示层
  var _tip, tID;

    if (!msg) return;
    _tip && _tip.remove() && clearTimeout(tID);
    _tip = $('<div class="tips_overlay">' +'<div class="tipBox">' +'<div class="msg"><h2><i></i>' + msg + '</h2></div>' +'</div>' +'</div>').appendTo(".wrapper");
    setTimeout(function () {
      _tip.addClass('overlay-in');
      _tip.children(".tipBox").addClass('mask-in');

    }, 10);
    tID = setTimeout(function () {
      if (!_tip) return;
      _tip.addClass('overlay-out');
      _tip.children(".tipBox").addClass('mask-out');
      _tip.remove();

      _tip = null;
    }, 2000);
  
  return tipBox;
}
/**
 * 获取用户id
 */
const getUserId = () => fetchJsonp('//my.' + uleUrl + '/usr/getIndexCookies.do', {} , { jsonpCallback: 'jsonCallBack' })
// export const Browser = browserFunc()
export default{
  browser,
  getUserId,
  getCookie,
  checkUserStatus,
  isLogin,  
  getQueryJson
}

